//
//  XHPaymentBouncedViewController.m
//  umibus
//
//  Created by 冷山 on 16/6/25.
//  Copyright © 2016年 lexingsoft. All rights reserved.
//

#import "XHPaymentBouncedViewController.h"
#import "XHTrafficPackageViewController.h"
#import "XHMyTrafficMoneyViewController.h"
#import "XHEditorPassWordViewController.h"
#import "XHTabBarController.h"

#import "Services.h"
#import "AppDelegate.h"


#define SCREEN_HEIGHT                   ([UIScreen mainScreen].bounds.size.height)
#define SCREEN_WIDTH                    ([UIScreen mainScreen].bounds.size.width)


@interface XHPaymentBouncedViewController ()

@property (weak, nonatomic) IBOutlet UITextField *ui_textField;
@property (weak, nonatomic) IBOutlet UIButton *ui_forgetPassWord;
@property (weak, nonatomic) IBOutlet UIButton *ui_ok;
@property (weak, nonatomic) IBOutlet UIButton *ui_cancel;
@property (weak, nonatomic) IBOutlet UIView *ui_bgView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *la_ViewBottom;

@property (strong, nonatomic) NSTimer *timer;
@property (strong, nonatomic) NSString * passWord;

@end

@implementation XHPaymentBouncedViewController

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.ui_bgView.layer.masksToBounds = YES;
    self.ui_bgView.layer.cornerRadius = 8;
    
    self.ui_textField.layer.borderColor = [ColorManager colorWithHexString:@"#9F9F9F"].CGColor;
    self.ui_textField.layer.borderWidth = 0.5;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideNotification:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextDidChangeNotifications:) name:UITextFieldTextDidChangeNotification object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self animation];
}

- (void)animation
{
    CGPoint startPoint = CGPointMake((SCREEN_WIDTH-270)/2, -(SCREEN_HEIGHT-180)/2);
    self.ui_bgView.layer.position=startPoint;
    
    //damping:阻尼，范围0-1，阻尼越接近于0，弹性效果越明显
    //velocity:弹性复位的速度
    [UIView animateWithDuration:.8 delay:0 usingSpringWithDamping:.5 initialSpringVelocity:1.0 options:UIViewAnimationOptionCurveLinear animations:^{
        CGPoint startPoint = CGPointMake((SCREEN_WIDTH-270)/2, (SCREEN_HEIGHT-180)/2 +180);
        self.ui_bgView.layer.position=startPoint;
        self.la_ViewBottom.constant = (SCREEN_HEIGHT-180)/2;
    } completion:^(BOOL finished) {
        
    }];
}


- (IBAction)okAction:(id)sender
{
    if (![NSString checkNullString:self.submitToken])
    {
        if (![NSString checkNullString:self.passWord])
        {
            [self userTrafficPackageWithOrderSn:self.orderSn userCount:self.conut phone:self.phone passWord:self.ui_textField.text phoneLocation:self.phoneLocation submitToken:self.submitToken];
        }
        else
        {
            [PromptAction showInfoPrompt:@"请输入密码"];
        }
    }
    else
    {
        if (![NSString checkNullString:self.passWord])
        {
            [self userFinancialBenefitsWithPhone:self.phone phoneLocation:self.phoneLocation fundProductId:self.fundProductId chargeCount:self.conut];
        }
        else
        {
            [PromptAction showInfoPrompt:@"请输入密码"];
        }

    }
}
- (IBAction)cancelAction:(id)sender
{
    [UIView animateWithDuration:0.35 animations:^{
        
        self.la_ViewBottom.constant = -350;
        [self.view layoutIfNeeded];
    }];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.35
                                                  target:self selector:@selector(cancelViewController) userInfo:nil repeats:YES];
}

//忘记密码
- (IBAction)forgetPassWordAction:(id)sender
{
    [self dismissViewControllerAnimated:NO completion:^{
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"editorPassWordViewController" object:nil];

    }];
}
- (void)cancelViewController
{
    [self dismissViewControllerAnimated:NO completion:^{
        
    }];
}

//使用理财流量包
- (void)userFinancialBenefitsWithPhone:(NSString*)phone
                         phoneLocation:(NSString*)phoneLocation
                         fundProductId:(NSString*)fundProductId
                           chargeCount:(NSString*)chargeCount
{
    [PromptAction showWaitPrompt:@"正在加载..."];
    [ServiceManager userFinancialBenefitsWithPhone:phone phoneLocation:phoneLocation fundProductId:fundProductId chargeCount:chargeCount success:^(NSDictionary *data) {
        [PromptAction dismissPrompt];
        if ([chargeCount integerValue]==self.totalRemainCount)
        {
           //全部使用完
            AppDelegate * delegate = [UIApplication sharedApplication].delegate;
            [delegate.tabBarController goMyMoneyFlow:1];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
        else
        {
            //还有剩余
            AppDelegate * delegate = [UIApplication sharedApplication].delegate;
            [delegate.tabBarController goMyMoneyFlow:0];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
        [UIView animateWithDuration:0.35 animations:^{
            
            self.la_ViewBottom.constant = SCREEN_HEIGHT;
            [self.view layoutIfNeeded];
        }];
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.35
                                                      target:self selector:@selector(dissMissViewController) userInfo:nil repeats:YES];
       
    } failure:^(NSDictionary *data) {
    
    }];
}
//使用流量屯包
- (void)userTrafficPackageWithOrderSn:(NSString*)orderSn
                            userCount:(NSString*)userCount
                                phone:(NSString*)phone
                             passWord:(NSString*)passWord
                        phoneLocation:(NSString*)phoneLocation
                          submitToken:(NSString*)submitToken;
{
    [PromptAction showWaitPrompt:@"正在加载"];
    [ServiceManager userTrafficPackageWithOrderSn:orderSn userCount:userCount phone:phone passWord:passWord phoneLocation:phoneLocation submitToken:submitToken success:^(NSDictionary *data) {
        [PromptAction dismissPrompt];
        [UIView animateWithDuration:0.35 animations:^{
            
            self.la_ViewBottom.constant = SCREEN_HEIGHT;
            [self.view layoutIfNeeded];
        }];
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.35
                                                      target:self selector:@selector(dissMissViewController) userInfo:nil repeats:YES];
    } failure:^(NSDictionary *data) {
        
    }];
}

- (void)dissMissViewController
{
    [self dismissViewControllerAnimated:NO completion:^{
        [[NSNotificationCenter defaultCenter] postNotificationName:@"popToViewController" object:nil];
    }];
}

#pragma mark - TextFieldNotification

- (void)textFieldTextDidChangeNotifications:(NSNotification *)notif
{
    UITextField * textField = (UITextField *)[notif object];
    self.passWord = textField.text;
}


#pragma mark - KeyBoardNotification
- (void)keyboardWillShowNotification:(NSNotification *)notif{
    float animationTime=[[notif.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    CGFloat keyboardHeight=[notif.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
    WS(weakSelf)
    [UIView animateWithDuration:animationTime animations:^{
        CGFloat constant=weakSelf.la_ViewBottom.constant;
        weakSelf.la_ViewBottom.constant=keyboardHeight > constant ? SCREEN_HEIGHT-(180+keyboardHeight) : constant;
        [weakSelf.view layoutIfNeeded];
    }];
}

- (void)keyboardWillHideNotification:(NSNotification *)notif{
    float animationTime=[[notif.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    WS(weakSelf)
    [UIView animateWithDuration:animationTime animations:^{
        weakSelf.la_ViewBottom.constant=0;
        [weakSelf.view layoutIfNeeded];
    }];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
